const { DataTypes } = require('sequelize');
const sequelize = require('../config/database');
const User = require('./User');

const Moment = sequelize.define('Moment', {
    id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    userId: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: {
            model: 'Users',
            key: 'id'
        }
    },
    content: {
        type: DataTypes.TEXT,
        allowNull: true
    },
    images: {
        type: DataTypes.JSON,
        allowNull: true,
        defaultValue: []
    },
    likes: {
        type: DataTypes.JSON,
        allowNull: true,
        defaultValue: []
    },
    comments: {
        type: DataTypes.JSON,
        allowNull: true,
        defaultValue: []
    },
    timestamp: {
        type: DataTypes.DATE,
        defaultValue: DataTypes.NOW
    }
});

// 建立与User模型的关联
Moment.belongsTo(User, {
    foreignKey: 'userId',
    onDelete: 'CASCADE'
});

// 自动同步数据库
Moment.sync({ alter: true })
    .then(() => {
        console.log('Moment 模型同步完成');
    })
    .catch(error => {
        console.error('Moment 模型同步失败:', error);
    });

module.exports = Moment; 